package edu.kit.csl.pisa.models;

/*
This file is part of the PISA Alignment Tool.

Copyright (C) 2013
Karlsruhe Institute of Technology
Cognitive Systems Lab (CSL)
Felix Stahlberg

PISA is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PISA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PISA. If not, see <http://www.gnu.org/licenses/>.
*/

import edu.kit.csl.pisa.datatypes.Dictionary;
import edu.kit.csl.pisa.datatypes.SentenceAlignment;

/**
 * This class represents a IBM Model 3 instance. This is normally used for
 * initializing Model 3P parameters, but not in EM training. It can be loaded
 * from the file system (GIZA++ format) via {@link #loadFromFilesystem()}.
 * Model parameters are represented as n-dimensional double arrays in the order
 * in which they occur in GIZA++ files. See 
 * {@link http://code.google.com/p/giza-pp/source/browse/trunk/GIZA%2B%2B-v2/README}
 * for more information.
 */
public class Model3 extends AlignmentModel {

	// Model parameters
	double[][] t;
	double[][] n;
	double[][][][] d;
	double p1;
	
	public Model3(String name, double[][] t, double[][] n, double[][][][] d,
			double p1) {
		super(name);
		this.t = t;
		this.n = n;
		this.d = d;
		this.p1 = p1;
	}

	/**
	 * Not implemented because IBM Model 4 is not used in training.
	 */
	@Override
	public double calculateAlignmentProbability(SentenceAlignment a) {
		assert false;
		return 0;
	}

	/**
	 * Not implemented because IBM Model 4 is not used in training.
	 */
	@Override
	public void initializeFractionalCounts() {
		assert false;
	}
	
	/**
	 * Not implemented because IBM Model 4 is not used in training.
	 */
	@Override
	public void writeBackFractionalCounts() {
		assert false;
	}
	
	/**
	 * Not implemented because IBM Model 4 is not used in training.
	 */
	@Override
	public void aggregateFractionalCount(SentenceAlignment a,
			double weight) {
		assert false;
	}
	
	/**
	 * Get lexical translation parameters.
	 * 
	 * @return 2-dimensional table of lexical translation probabilities (t)
	 */
	public double[][] getParameterT() {
		return t;
	}

	/**
	 * Get fertility distribution parameters.
	 * 
	 * @return the 2-dimensional table of fertility probabilities (n)
	 */
	public double[][] getParameterN() {
		return n;
	}

	/**
	 * Get the distortion parameters.
	 * 
	 * @return the 4-dimensional table of distortion probabilities (d)
	 */
	public double[][][][] getParameterD() {
		return d;
	}

	/**
	 * Get the p1 parameter (probability of an empty word insertion).
	 * 
	 * @return the p1 parameter
	 */
	public double getParameterP1() {
		return p1;
	}
	
	/**
	 * t, n, d are written to the file system.
	 * {@inheritDoc}
	 */
	@Override
	public void dumpToFilesystem(String prefix, String postfix) {
		Dictionary srcDict = Dictionary.getSingleton(Dictionary.SRC_DICT);
		Dictionary trgtDict = Dictionary.getSingleton(Dictionary.TRGT_DICT);
		this.dumpParameter(prefix + "t" + postfix, t);
		this.dumpParameter(prefix + "actual.t" + postfix, t, new Dictionary[]{
			srcDict, trgtDict});
		this.dumpParameter(prefix + "n" + postfix, n);
		this.dumpParameter(prefix + "actual.n" + postfix, n, new Dictionary[]{
			srcDict, null});
		this.dumpParameter(prefix + "d" + postfix, d);
	}
}
